package com.nutiteq.kmlzzz;

import java.io.IOException;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.game.Sprite;

import com.nutiteq.MapComponent;
import com.nutiteq.kml.KmlService;
import com.nutiteq.kml.KmlUrlReader;

public class ServicesList extends List implements CommandListener {
  private final Command back = new Command("Back", Command.BACK, 0);
  private final Command done = new Command("Done", Command.ITEM, 0);
  private final Command select = new Command("Select", Command.OK, 0);

  private static final KmlUrlReader PANO = new KmlUrlReader(
      "http://www.panoramio.com/panoramio.kml?LANG=en_US.utf8&", true);
  private static final KmlUrlReader POLY_LINE = new KmlUrlReader("/poly-line.kml", false);
  private final KmlCanvas canvas;
  private final MapComponent component;

  private final boolean hadPanoramio;

  private Image unChecked;
  private Image checked;
  private int checkedIndex = -1;
  private final boolean hadJared;

  public ServicesList(final MapComponent component, final KmlCanvas canvas) {
    super("Services", List.IMPLICIT);
    this.component = component;
    this.canvas = canvas;
    final KmlService[] services = component.getKmlServices();

    try {
      final Image checedCombined = Image.createImage("/check.png");
      unChecked = Image.createImage(checedCombined, 0, 0, 16, 16, Sprite.TRANS_NONE);
      checked = Image.createImage(checedCombined, 16, 0, 16, 16, Sprite.TRANS_NONE);
    } catch (final IOException ignore) {
      ignore.printStackTrace();
    }

    append("Panoramio popular", unChecked);
    append("jared kml sample", unChecked);

    hadPanoramio = containsService(PANO, services);
    hadJared = containsService(POLY_LINE, services);
    checkedIndex = -1;
    if (hadPanoramio) {
      checkedIndex = 0;
      set(checkedIndex, getString(checkedIndex), checked);
    } else if (hadJared) {
      checkedIndex = 1;
      set(checkedIndex, getString(checkedIndex), checked);
    }

    setSelectCommand(select);
    addCommand(back);
    addCommand(select);
    addCommand(done);

    setCommandListener(this);
  }

  public void commandAction(final Command cmd, final Displayable d) {
    if (cmd == select) {
      if (checkedIndex == -1) {
        checkedIndex = getSelectedIndex();
      } else if (checkedIndex == getSelectedIndex()) {
        set(getSelectedIndex(), getString(getSelectedIndex()), unChecked);
        checkedIndex = -1;
      } else {
        set(checkedIndex, getString(checkedIndex), unChecked);
        checkedIndex = getSelectedIndex();
      }

      if (checkedIndex != -1) {
        set(checkedIndex, getString(checkedIndex), checked);
      }

      return;
    } else if (cmd == done) {
      checkState(0, PANO, hadPanoramio);
      checkState(1, POLY_LINE, hadJared);
    }
    Display.getDisplay(ShowKml.instance).setCurrent(canvas);
  }

  private void checkState(final int index, final KmlService service, final boolean hadBefore) {
    final boolean isSelected = checkedIndex == index;
    if (isSelected && !hadBefore) {
      component.addKmlService(service);
    } else if (!isSelected && hadBefore) {
      component.removeKmlService(service);
      if (service instanceof KmlUrlReader) {
        ((KmlUrlReader) service).resetRead();
      }
    }
  }

  private boolean containsService(final KmlService service, final KmlService[] services) {
    if (services.length == 0) {
      return false;
    }

    for (int i = 0; i < services.length; i++) {
      if (services[i] == service) {
        return true;
      }
    }

    return false;
  }
}
